﻿using System;
using System.Collections.Generic;
using Agnes.Core;
using Agnes.TaskManager.Models;
using Csla;
using Csla.Core;
using System.ComponentModel;

namespace Agnes.TaskManager
{
    /// <summary>
    /// 新建任务ViewModel
    /// </summary>
    class TaskInfoEditViewModel
        :ScreenBase<TaskInfo>
    {
        /// <summary>
        /// true:新建TaskInfo
        /// false:修改TaskInfo
        /// </summary>
        private bool _isCreate = true; 

        #region Binding Property

        /// <summary>
        /// CirculateTypeNVL
        /// </summary>
        public CirculateTypeNVL CirculateTypeNVL { get; private set; }

        /// <summary>
        /// PriorityLevelNVL
        /// </summary>
        public PriorityLevelNVL PriorityLevelNVL { get; private set; }

        /// <summary>
        /// 任务标记列表
        /// </summary>
        public List<TaskFlagItem> TaskFlags { get; private set; }

        /// <summary>
        /// 确定按钮上显示的字体
        /// </summary>
        public String OKContent { get; private set; }

        #endregion

        /// <summary>
        /// 构造函数 新建任务 使用
        /// </summary>
        public TaskInfoEditViewModel()
        {
            Model = TaskInfo.CreateAsChild();

            InitViewModel();
            OKContent = "创建任务";
        }

        /// <summary>
        /// 构造函数 编辑任务 使用
        /// </summary>
        /// <param name="model"></param>
        public TaskInfoEditViewModel(TaskInfo model)
        {
            Model = model;

            InitViewModel();
            OKContent = "修改任务";
            _isCreate = false;
        }

        #region OK

        private bool _canOK = true;
        /// <summary>
        /// CanOK
        /// </summary>
        public bool CanOK
        {
            get { return _canOK; }
            set
            {
                _canOK = value;
                NotifyOfPropertyChange(() => CanOK);
            }
        }

        /// <summary>
        /// OK
        /// </summary>
        public void OK()
        {
            Model.ApplyEdit();
            if (_isCreate)
                TaskManager.Instance.TaskRoots.Add(Model);
            TaskManager.Instance.Save();
            AgnesWorkBenchService.CloseDocument(this.GetHashCode());
        }

        #endregion

        #region Cancel

        /// <summary>
        /// Cancel
        /// </summary>
        public void Cancel()
        {
            Model.CancelEdit();

            AgnesWorkBenchService.CloseDocument(this.GetHashCode());
        }

        public void OnClosing(object sender, CancelEventArgs e)
        {
            if (Model.IsDirty)
            {
                SaveDialog sd = new SaveDialog();
                if (Model.IsValid)
                {
                    sd.FileTags.Add("新建任务 " + Model.TaskName);
                }
                else
                {
                    sd.FileTags.Add("[有错误的]新建任务 " + Model.TaskName);
                }

                sd.CanSave &= Model.IsValid;
                sd.ShowDialog();
                if (sd.SaveResult == Core.SaveResult.Save)
                {
                    Model.ApplyEdit();
                    if (_isCreate)
                        TaskManager.Instance.TaskRoots.Add(Model);
                    TaskManager.Instance.Save();
                }
                else if (sd.SaveResult == Core.SaveResult.DoNotSave)
                {
                    Model.CancelEdit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }

        #endregion

        private void InitViewModel()
        {
            Model.BeginEdit();
            CirculateTypeNVL = DataPortal.Fetch<CirculateTypeNVL>();
            PriorityLevelNVL = DataPortal.Fetch<PriorityLevelNVL>();
            TaskFlags = new List<TaskFlagItem>();
            LoadTaskFlag();
            CanOK = Model.IsValid;
            Model.PropertyChanged += ModelPropertyChanged;
            Model.ChildChanged += ModelChildChanged;
        }

        void ModelChildChanged(object sender, ChildChangedEventArgs e)
        {
            CanOK = Model.IsValid;
        }

        void ModelPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            CanOK = Model.IsValid;
        }

        protected override object CreateView()
        {
            return new TaskInfoEditView();
        }

        private void LoadTaskFlag()
        {
            TaskFlagNVL nvl = DataPortal.Fetch<TaskFlagNVL>();
            foreach (String str in Enum.GetNames(typeof(TaskFlag)))
            {
                TaskFlagItem tfi = new TaskFlagItem();
                tfi.TaskFlag = (TaskFlag)Enum.Parse(typeof(TaskFlag), str);
                tfi.TaskFlagStr = nvl.GetItemByKey(tfi.TaskFlag).Value;
                tfi.IcoUri = "pack://application:,,,/Agnes.Resources;component/Image/Agnes.TaskManager/TaskFlag/"+tfi.TaskFlag+".png";
                TaskFlags.Add(tfi);
            }
        }
    }

    /// <summary>
    /// 用于TaskFlag Combox的数据绑定
    /// </summary>
    internal class TaskFlagItem
    {
        /// <summary>
        /// TaskFlag
        /// </summary>
        public TaskFlag TaskFlag { get; set; }

        /// <summary>
        /// 显示字符串
        /// </summary>
        public string TaskFlagStr { get; set; }

        /// <summary>
        /// 图标Uri
        /// </summary>
        public string IcoUri { get; set; }
    }
}
